package gold.digger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC833 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
            int N = S.length();
            int[] match = new int[N];
            Arrays.fill(match, -1);

            for (int i = 0; i < indexes.length; ++i) {
                int ix = indexes[i];
                if (S.substring(ix, ix + sources[i].length()).equals(sources[i]))
                    match[ix] = i;
            }

            StringBuilder ans = new StringBuilder();
            int ix = 0;
            while (ix < N) {
                if (match[ix] >= 0) {
                    ans.append(targets[match[ix]]);
                    ix += sources[match[ix]].length();
                } else {
                    ans.append(S.charAt(ix++));
                }
            }
            return ans.toString();
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC833 an = new LC833();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
